using System;
using System.IO;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
using System.Linq;

namespace GPUAPP2
{
    public class YDataRow : INotifyPropertyChanged
    {
        private int rowNumber;
        private int f0;
        private int f1;
        private int f2;
        private string sortedValues;

        public int RowNumber
        {
            get => rowNumber;
            set
            {
                if (rowNumber != value)
                {
                    rowNumber = value;
                    OnPropertyChanged();
                }
            }
        }

        public int F0
        {
            get => f0;
            set
            {
                if (f0 != value)
                {
                    f0 = value;
                    OnPropertyChanged();
                }
            }
        }

        public int F1
        {
            get => f1;
            set
            {
                if (f1 != value)
                {
                    f1 = value;
                    OnPropertyChanged();
                }
            }
        }

        public int F2
        {
            get => f2;
            set
            {
                if (f2 != value)
                {
                    f2 = value;
                    OnPropertyChanged();
                }
            }
        }

        public string SortedValues
        {
            get => sortedValues;
            set
            {
                if (sortedValues != value)
                {
                    sortedValues = value;
                    OnPropertyChanged();
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class FDataRow : INotifyPropertyChanged
    {
        private int rowNumber;
        private int[] fValues;

        public FDataRow()
        {
            fValues = new int[30];
        }

        public int RowNumber
        {
            get => rowNumber;
            set
            {
                if (rowNumber != value)
                {
                    rowNumber = value;
                    OnPropertyChanged();
                }
            }
        }

        public int[] FValues
        {
            get => fValues;
            set
            {
                if (fValues != value)
                {
                    fValues = value;
                    OnPropertyChanged();
                }
            }
        }

        public int GetFValue(int index)
        {
            return fValues[index];
        }

        private void SetFValue(int index, int value)
        {
            if (fValues[index] != value)
            {
                fValues[index] = value;
                OnPropertyChanged($"F{index}");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class DataManager
    {
        private string filePath;
        private int[] yData;
        private int[] yRowInfo;
        private bool isDataLoaded;
        private ObservableCollection<YDataRow> yDataRows;
        private ObservableCollection<FDataRow> fDataRows;
        private HashSet<string> sortedYValues;

        public string FilePath
        {
            get => filePath;
            private set => filePath = value;
        }

        public int[] YData
        {
            get => yData;
            private set => yData = value;
        }

        public int[] YRowInfo
        {
            get => yRowInfo;
            private set => yRowInfo = value;
        }

        public bool IsDataLoaded
        {
            get => isDataLoaded;
            private set => isDataLoaded = value;
        }

        public int RowCount
        {
            get => IsDataLoaded ? YData.Length / 3 : 0;
        }

        public ObservableCollection<YDataRow> YDataRows
        {
            get => yDataRows;
            private set => yDataRows = value;
        }

        public ObservableCollection<FDataRow> FDataRows
        {
            get => fDataRows;
            private set => fDataRows = value;
        }

        public HashSet<string> SortedYValues
        {
            get => sortedYValues;
            private set => sortedYValues = value;
        }

        public DataManager()
        {
            Reset();
        }

        public void Reset()
        {
            filePath = null;
            yData = null;
            yRowInfo = null;
            isDataLoaded = false;
            YDataRows = new ObservableCollection<YDataRow>();
            FDataRows = new ObservableCollection<FDataRow>();
            SortedYValues = new HashSet<string>();
        }

        public bool LoadData(string filePath)
        {
            try
            {
                FilePath = filePath;
                var lines = File.ReadAllLines(filePath);
                YDataRows = new ObservableCollection<YDataRow>();
                SortedYValues = new HashSet<string>();
                
                // 初始化YData数组
                YData = new int[lines.Length * 3];
                YRowInfo = new int[lines.Length];

                for (int i = 0; i < lines.Length; i++)
                {
                    var values = lines[i].Split('-').Select(int.Parse).ToArray();
                    var row = new YDataRow
                    {
                        RowNumber = i + 1,
                        F0 = values[0],
                        F1 = values[1],
                        F2 = values[2]
                    };

                    // 排序并连接值
                    var sortedValues = new[] { values[0], values[1], values[2] };
                    Array.Sort(sortedValues);
                    row.SortedValues = string.Join("", sortedValues);
                    SortedYValues.Add(row.SortedValues);

                    YDataRows.Add(row);

                    // 设置YData数组
                    YData[i * 3] = values[0];
                    YData[i * 3 + 1] = values[1];
                    YData[i * 3 + 2] = values[2];
                    YRowInfo[i] = i + 1;
                }

                isDataLoaded = true;
                return true;
            }
            catch (Exception ex)
            {
                isDataLoaded = false;
                throw new Exception($"加载数据失败: {ex.Message}");
            }
        }

        public string GetDataSample()
        {
            if (!IsDataLoaded || YData == null || YData.Length < 3)
            {
                return "无数据";
            }

            return $"{YData[0]}-{YData[1]}-{YData[2]}";
        }

        public void ValidateData()
        {
            if (!IsDataLoaded)
            {
                throw new InvalidOperationException("数据未加载");
            }

            if (YData == null || YRowInfo == null)
            {
                throw new InvalidOperationException("数据数组为空");
            }

            if (YData.Length != YRowInfo.Length * 3)
            {
                throw new InvalidOperationException("数据格式错误");
            }
        }
    }
} 